#!/bin/bash
user=root
userPWD=9dspod8a
database=GotoMyCloudDB
/usr/bin/mysqladmin -u$user -p$userPWD flush-logs
daily_databakDir=/srv/bakmysql/daily
mysqlDataDir=/srv/mysql
eMailFile=$daily_databakDir/email.txt
eMail=xia.yan@cloudsoar.com
DATE=`date +%Y%m%d`
logFile=$daily_databakDir/mysql$DATE.log
echo " " > $eMailFile
echo "-----------------------" >> $eMailFile
echo $(date +"%y-%m-%d %H:%M:%S") >> $eMailFile
echo "-------
------------------" >> $eMailFile
TIME=$(date "-d 10 day ago" +%Y%m%d%H%M%S)
StartTime=$(date "-d 1 day ago" +"%Y-%m-%d %H:%M:%S")
echo “Delete 10 days before the log” >>$eMailFile
mysql -u$user -p$userPWD -e "purge master logs before ${TIME}" && echo "delete 10 days before log" |tee -a $eMailFile
filename=`cat $mysqlDataDir/mysqld-bin.index |awk -F "/" '{print $2}'`
for i in $filename
do
echo "$StartTime start backup binlog" >> $eMailFile
mysqlbinlog -u$user -p$userPWD -d $database --start-datetime="$StartTime" $mysqlDataDir/$i >> $daily_databakDir/daily$DATE.sql |tee -a $eMailFile
done
if [ $? = 0 ]
then
find $daily_databakDir -name "*.log" -type f -mtime +32 -exec rm {} \; > /dev/null 2>&1
cd $daily_databakDir
echo "Daily backup succeed" >> $eMailFile
else
echo "Daily backup fail" >> $eMailFile
mail -s "MySQL Backup" $eMail < $eMailFile
fi
cat $eMailFile > $logFile
find $daily_databakDir -name "*.log" -type f -mtime +32 -exec rm {} \; > /dev/null 2>&